package cfca.seal.front.service.ws.util;

import cfca.sadk.algorithm.common.PKIException;
import cfca.sadk.util.KeyUtil;
import cfca.seal.exception.CodeException;
import cfca.seal.front.service.ws.bean.AddBlankSignatureBean;
import cfca.seal.front.service.ws.bean.AddWaterMarkToPdfBean;
import cfca.seal.front.service.ws.bean.AutoGenerateImageBean;
import cfca.seal.front.service.ws.bean.ClientSealBean;
import cfca.seal.front.service.ws.bean.MakeAndBindClientSealBean;
import cfca.seal.front.service.ws.bean.NameSealInfoBean;
import cfca.seal.front.service.ws.bean.OfficeWordSealBean;
import cfca.seal.front.service.ws.bean.PdfSealBean;
import cfca.seal.front.service.ws.bean.SealAutoCrossPdfBean;
import cfca.seal.front.service.ws.bean.SealInfoBean;
import cfca.seal.front.service.ws.bean.SignBean;
import cfca.seal.front.service.ws.bean.VerifyBean;
import cfca.seal.front.service.ws.bean.WebSealBean;
import cfca.seal.util.SealCertUtil;
import cfca.seal.util.StringUtil;

public class ValidityUtil
{
  public static void checkParamForSealInfo(SealInfoBean sealInfoBean)
    throws CodeException
  {
    checkSealPerson(sealInfoBean.getSealPerson());
    checkSealCode(sealInfoBean.getSealCode());
    checkSealName(sealInfoBean.getSealName());
    checkSealPassword(sealInfoBean.getSealPassword());
    checkSealPasswordValid(sealInfoBean.getSealPassword());
    checkSealType(sealInfoBean.getSealType());
    checkOrgCode(sealInfoBean.getOrgCode());
    checkSealOrg(sealInfoBean.getSealOrg());
  }

  public static void checkParamForNameSealInfo(NameSealInfoBean nameSealInfoBean) throws CodeException {
    checkSealPerson(nameSealInfoBean.getSealPerson());
    checkSealCode(nameSealInfoBean.getSealCode());
    checkSealName(nameSealInfoBean.getSealName());
    checkSealPassword(nameSealInfoBean.getSealPassword());
    checkSealPasswordValid(nameSealInfoBean.getSealPassword());
    checkImageShape(nameSealInfoBean.getImageShape());
    checkImageHeight(nameSealInfoBean.getImageHeight());
    checkImageWidth(nameSealInfoBean.getImageWidth());
    checkOrgCode(nameSealInfoBean.getOrgCode());
    checkSealOrg(nameSealInfoBean.getSealOrg());
  }

  public static void checkParamForSealAutoPdf(PdfSealBean pdfSealBean) throws CodeException {
    checkSealCode(pdfSealBean.getSealCode());
    checkSealPassword(pdfSealBean.getSealPassword());
    checkType(pdfSealBean.getType());
    checkOffsetXY(pdfSealBean.getOffsetX(), pdfSealBean.getOffsetY());
    checkKeyword(pdfSealBean.getType(), pdfSealBean.getKeyword());
    checkLocationStyle(pdfSealBean.getType(), pdfSealBean.getLocationStyle());
  }

  public static void checkParamForSealPdfFunction(PdfSealBean pdfSealBean) throws CodeException {
    checkType(pdfSealBean.getType());
    checkOffsetXY(pdfSealBean.getOffsetX(), pdfSealBean.getOffsetY());
    checkKeyword(pdfSealBean.getType(), pdfSealBean.getKeyword());
    checkLocationStyle(pdfSealBean.getType(), pdfSealBean.getLocationStyle());
  }

  public static void checkParamForVerify(VerifyBean verifyBean) throws CodeException {
    checkVerifyType(verifyBean.getVerifyType());
  }

  public static void checkParamForSealAutoCrossPdf(SealAutoCrossPdfBean sealAutoCrossPdfBean) throws CodeException {
    checkSealCode(sealAutoCrossPdfBean.getSealCode());
    checkSealPassword(sealAutoCrossPdfBean.getSealPassword());
    checkFromPage(sealAutoCrossPdfBean.getFromPage());
    checkToPage(sealAutoCrossPdfBean.getToPage());
    checkCrossStyle(sealAutoCrossPdfBean.getCrossStyle());
  }

  public static void checkParamForAddWaterMark2Pdf(AddWaterMarkToPdfBean addWaterMarkToPdfBean) throws CodeException {
    checkFromPage(addWaterMarkToPdfBean.getFromPage());
    checkToPage(addWaterMarkToPdfBean.getToPage());
    checkAlpha(addWaterMarkToPdfBean.getAlpha());
    checkRotationDegree(addWaterMarkToPdfBean.getRotationDegree());
    checkWaterMark(addWaterMarkToPdfBean.getWaterMarkImage(), addWaterMarkToPdfBean.getWaterMarkText());
    checkAbsoluteAndFit(addWaterMarkToPdfBean.getWaterMarkImage(), addWaterMarkToPdfBean.getWaterMarkText(), addWaterMarkToPdfBean.getAbsoluteX(), addWaterMarkToPdfBean.getAbsoluteY(), addWaterMarkToPdfBean.getFitWidth(), addWaterMarkToPdfBean.getFitHeight(), addWaterMarkToPdfBean.getTextFontSize());
  }

  public static void checkParamForSignWebSeal(WebSealBean webSealBean)
    throws CodeException
  {
    checkSealCode(webSealBean.getSealCode());
    checkSealPassword(webSealBean.getSealPassword());
  }

  public static void checkParamForGetBindedSealInfo(String certDN, String certSN) throws CodeException {
    checkCertDnAndSn(certDN, certSN);
  }

  public static void checkParamForSign(SignBean signBean) throws CodeException {
    checkSealCode(signBean.getSealCode());
    checkSealPassword(signBean.getSealPassword());
  }

  public static void checkParamForAutoGenerateImage(AutoGenerateImageBean autoGenerateImageBean) throws CodeException {
    checkImageName(autoGenerateImageBean.getImageName(), autoGenerateImageBean.getImageShape(), autoGenerateImageBean.getImageName2());
    checkImageShapeForAutoGenerateImage(autoGenerateImageBean.getImageShape());
    checkImageHeight(autoGenerateImageBean.getImageHeight());
    checkImageWidth(autoGenerateImageBean.getImageWidth());
    checkFontSize(autoGenerateImageBean.getFontSize());
  }

  public static void checkParamForMakeAndBindClientSeal(MakeAndBindClientSealBean makeAndBindClientSealBean) throws CodeException {
    checkSealPerson(makeAndBindClientSealBean.getSealPerson());
    checkSealOrg(makeAndBindClientSealBean.getSealOrg());
    checkSealName(makeAndBindClientSealBean.getSealName());
    checkSealCode(makeAndBindClientSealBean.getSealCode());
    checkSealPassword(makeAndBindClientSealBean.getSealPassword());
    checkCertDnAndSn(makeAndBindClientSealBean.getCertDn(), makeAndBindClientSealBean.getCertSn());
    checkUserName(makeAndBindClientSealBean.getName(), makeAndBindClientSealBean.getRealName());
  }

  public static void checkParamForClientSeal(ClientSealBean clientSealBean, int pdfMaxPage) throws CodeException {
    checkTypeForClientSeal(clientSealBean.getType());
    checkPageXY(clientSealBean.getPage(), clientSealBean.getLx(), clientSealBean.getLy(), pdfMaxPage);
    checkOffsetXY(clientSealBean.getOffsetX(), clientSealBean.getOffsetY());
    checkKeyword(clientSealBean.getType(), clientSealBean.getKeyword());
    checkLocationStyle(clientSealBean.getType(), clientSealBean.getLocationStyle());
  }

  public static void checkParamForOfficeWordSeal(OfficeWordSealBean officeWordSealBean) throws CodeException {
    checkSealCode(officeWordSealBean.getSealCode());
    checkSealPassword(officeWordSealBean.getSealPassword());
    checkKeyword(officeWordSealBean.getKeyword());
    checkSuggestedSigner(officeWordSealBean.getSuggestedSigner());
    checkSuggestedSignerPost(officeWordSealBean.getSuggestedSignerPost());
    checkSuggestedSignerEmail(officeWordSealBean.getSuggestedSignerEmail());
    checkSigningInstructions(officeWordSealBean.getSigningInstructions());
  }

  public static void checkParamForOfficeWordFunction(OfficeWordSealBean officeWordSealBean) throws CodeException {
    checkKeyword(officeWordSealBean.getKeyword());
    checkSuggestedSigner(officeWordSealBean.getSuggestedSigner());
    checkSuggestedSignerPost(officeWordSealBean.getSuggestedSignerPost());
    checkSuggestedSignerEmail(officeWordSealBean.getSuggestedSignerEmail());
    checkSigningInstructions(officeWordSealBean.getSigningInstructions());
    checkSignatureContent(officeWordSealBean.getType(), officeWordSealBean.getSignatureText(), officeWordSealBean.getSignatureImage());
  }

  public static void checkParamForAddBlankSignature2Pdf(AddBlankSignatureBean addBlankSignatureBean, int pdfMaxPage) throws CodeException {
    checkFieldName(addBlankSignatureBean.getFieldName());
    checkPageXY(addBlankSignatureBean.getPage(), addBlankSignatureBean.getLx(), addBlankSignatureBean.getLy(), pdfMaxPage);
    checkWidthAndHeight(addBlankSignatureBean.getWidth(), addBlankSignatureBean.getHeight());
  }

  private static void checkWidthAndHeight(String width, String height) throws CodeException {
    if (StringUtil.isEmpty(width)) {
      throw new CodeException("C3042", "空白域标签宽度没有输入");
    }
    if (StringUtil.isEmpty(height)) {
      throw new CodeException("C3042", "空白域标签高度没有输入");
    }
    if ((!StringUtil.isNumeric(width)) || (!StringUtil.isNumeric(height)))
      throw new CodeException("C1017", "空白签名域高度和宽度必须为整数");
  }

  private static void checkFieldName(String fieldName) throws CodeException {
    if (StringUtil.isEmpty(fieldName))
      throw new CodeException("C1016", "空白签名域名称不能为空");
  }

  private static void checkSignatureContent(String type, String signatureText, String signatureImage) throws CodeException
  {
    if ("StamperImageConfirmSignature".equals(type)) {
      if (StringUtil.isEmpty(signatureImage))
        throw new CodeException("C3080", "图章签名行方式签章，图片不能为空");
    } else if (("ConfirmSignature".equals(type)) && 
      (StringUtil.isEmpty(signatureText)) && (StringUtil.isEmpty(signatureImage)))
      throw new CodeException("C3079", "签名行方式签章，图片或文本必须输入其中一项");
  }

  private static void checkSigningInstructions(String signingInstructions)
    throws CodeException
  {
    if (StringUtil.isEmpty(signingInstructions))
      throw new CodeException("C3042", "签章说明没有输入");
  }

  private static void checkSuggestedSignerEmail(String suggestedSignerEmail) throws CodeException
  {
    if (StringUtil.isEmpty(suggestedSignerEmail))
      throw new CodeException("C3042", "签章人邮箱没有输入");
  }

  private static void checkSuggestedSignerPost(String suggestedSignerPost) throws CodeException
  {
    if (StringUtil.isEmpty(suggestedSignerPost))
      throw new CodeException("C3042", "签章人职位没有输入");
  }

  private static void checkSuggestedSigner(String suggestedSigner) throws CodeException
  {
    if (StringUtil.isEmpty(suggestedSigner))
      throw new CodeException("C3042", "签章人没有输入");
  }

  private static void checkKeyword(String keyword) throws CodeException
  {
    if (StringUtil.isEmpty(keyword))
      throw new CodeException("C3042", "关键字没有输入");
  }

  private static void checkTypeForClientSeal(String type) throws CodeException
  {
    if (StringUtil.isEmpty(type)) {
      throw new CodeException("C3042", "签章类型没有输入");
    }
    if (((!"2".equals(type) ? 1 : 0) & (!"3".equals(type) ? 1 : 0)) != 0)
      throw new CodeException("C3002", "签章类型参数不正确");
  }

  private static void checkKeyword(String type, String keyword) throws CodeException
  {
    if (("3".equals(type)) && (StringUtil.isEmpty(keyword)))
      throw new CodeException("C3006", "按关键字签章时，关键字不能为空");
  }

  private static void checkOffsetXY(String offsetX, String offsetY) throws CodeException
  {
    if ((!StringUtil.isNumeric(offsetX)) || (!StringUtil.isNumeric(offsetY)))
      throw new CodeException("C3059", "横轴/纵轴偏移只能输入数字");
  }

  private static void checkAlpha(String alpha) throws CodeException
  {
    if (StringUtil.isEmpty(alpha)) {
      throw new CodeException("C3042", "透明度没有输入");
    }
    float alphaFloat = 0.0F;
    try {
      alphaFloat = Float.parseFloat(alpha);
    } catch (Exception e) {
      throw new CodeException("C3058", "透明度取值范围0-1");
    }
    if ((alphaFloat < 0.0F) || (alphaFloat > 1.0F))
      throw new CodeException("C3058", "透明度取值范围0-1");
  }

  private static void checkRotationDegree(String rotationDegree) throws CodeException
  {
    if (StringUtil.isEmpty(rotationDegree)) {
      throw new CodeException("C3042", "旋转度没有输入");
    }
    if (!StringUtil.isNumeric(rotationDegree))
      throw new CodeException("C3057", "旋转度只能输入数字");
  }

  private static void checkUserName(String name, String realName) throws CodeException
  {
    if (StringUtil.isEmpty(name)) {
      throw new CodeException("C3042", "用户名没有输入");
    }
    if (StringUtil.isEmpty(realName))
      throw new CodeException("C3042", "姓名没有输入");
  }

  private static void checkImageName(String imageName, String imageShape, String imageName2) throws CodeException
  {
    if (StringUtil.isEmpty(imageName)) {
      throw new CodeException("C2004", "印章图片内容不能为空");
    }
    if (StringUtil.checkValueInValid(imageName)) {
      throw new CodeException("C1012", "印章内容不能包含特殊字符");
    }
    if (("2".equals(imageShape)) && (StringUtil.isEmpty(imageName2))) {
      throw new CodeException("C2004", "印章图片内容不能为空");
    }
    if ((!StringUtil.isEmpty(imageName2)) && (StringUtil.checkValueInValid(imageName2)))
      throw new CodeException("C1012", "印章内容不能包含特殊字符");
  }

  public static void checkFontSize(String fontSize) throws CodeException
  {
    if ((StringUtil.isNotEmpty(fontSize)) && (!StringUtil.isNumeric(fontSize)))
      throw new CodeException("C2010", "输入的字体大小不正确");
  }

  private static void checkCertDnAndSn(String certDN, String certSN) throws CodeException
  {
    if (StringUtil.isEmpty(certDN)) {
      throw new CodeException("C3042", "证书DN没有输入");
    }
    if (StringUtil.isEmpty(certSN))
      throw new CodeException("C3042", "证书SN没有输入");
  }

  private static void checkWaterMark(String waterMarkImage, String waterMarkText) throws CodeException
  {
    if ((StringUtil.isEmpty(waterMarkImage)) && (StringUtil.isEmpty(waterMarkText)))
      throw new CodeException("C3055", "水印文本和水印图片不能同时为空");
  }

  private static void checkCrossStyle(String crossStyle) throws CodeException
  {
    if (StringUtil.isEmpty(crossStyle)) {
      throw new CodeException("C3042", "骑缝章风格没有输入");
    }
    if (!StringUtil.isNumeric(crossStyle)) {
      throw new CodeException("C3053", "骑缝章风格只能输入数字");
    }
    if ((Integer.valueOf(crossStyle).intValue() < 1) || (Integer.valueOf(crossStyle).intValue() > 6))
      throw new CodeException("C3054", "骑缝章风格输入不正确");
  }

  private static void checkFromPage(String fromPage) throws CodeException
  {
    if (StringUtil.isEmpty(fromPage)) {
      throw new CodeException("C3042", "起始页没有输入");
    }
    if (!StringUtil.isNumeric(fromPage))
      throw new CodeException("C3051", "起始页只能输入数字");
  }

  private static void checkToPage(String toPage) throws CodeException
  {
    if (StringUtil.isEmpty(toPage)) {
      throw new CodeException("C3042", "结束页没有输入");
    }
    if (!StringUtil.isNumeric(toPage))
      throw new CodeException("C3052", "结束页只能输入数字");
  }

  private static void checkVerifyType(String verifyType) throws CodeException
  {
    if (StringUtil.isEmpty(verifyType)) {
      throw new CodeException("C3042", "验章（签）类型没有输入");
    }
    if (!StringUtil.isNumeric(verifyType)) {
      throw new CodeException("C3012", "验章（签）类型参数不正确");
    }
    if ((Integer.valueOf(verifyType).intValue() < 0) || (Integer.valueOf(verifyType).intValue() > 3))
      throw new CodeException("C3012", "验章（签）类型参数不正确");
  }

  private static void checkType(String type) throws CodeException
  {
    if (StringUtil.isEmpty(type)) {
      throw new CodeException("C3042", "签章类型没有输入");
    }
    if (!"1".equals(type)) if (((!"2".equals(type) ? 1 : 0) & (!"3".equals(type) ? 1 : 0)) != 0)
        throw new CodeException("C3002", "签章类型参数不正确"); 
  }

  private static void checkImageWidth(String imageWidth)
    throws CodeException
  {
    if (StringUtil.isEmpty(imageWidth)) {
      throw new CodeException("C3042", "图片宽度没有输入");
    }
    int width = 0;
    try {
      width = Integer.parseInt(imageWidth);
    } catch (Exception e) {
      throw new CodeException("C9007", "图片宽度只能输入整数");
    }
    if ((width <= 0) || (width > 200))
      throw new CodeException("C2007", "图片宽度必须大于0小于200");
  }

  private static void checkImageHeight(String imageHeight) throws CodeException
  {
    if (StringUtil.isEmpty(imageHeight)) {
      throw new CodeException("C3042", "图片高度没有输入");
    }
    int height = 0;
    try {
      height = Integer.parseInt(imageHeight);
    } catch (Exception e) {
      throw new CodeException("C9006", "图片高度只能输入整数");
    }
    if ((height <= 0) || (height > 200))
      throw new CodeException("C2006", "图片高度必须大于0小于200");
  }

  private static void checkSealPerson(String sealPerson) throws CodeException
  {
    if (StringUtil.isEmpty(sealPerson)) {
      throw new CodeException("C2003", "制章人不能为空");
    }
    if (StringUtil.checkValueInValid(sealPerson)) {
      throw new CodeException("C1012", "制章人不能包含特殊字符");
    }
    if (sealPerson.length() > 32)
      throw new CodeException("C3046", "制章人最大输入长度不能超过32位");
  }

  public static void checkSealCode(String sealCode) throws CodeException
  {
    if (StringUtil.isEmpty(sealCode)) {
      throw new CodeException("C3004", "印章编码不能为空");
    }
    if (StringUtil.checkValueInValid2(sealCode)) {
      throw new CodeException("C3038", "印章编码只能包括数字字母下划线");
    }
    if (sealCode.length() > 32)
      throw new CodeException("C3049", "印章编码最大输入长度不能超过32位");
  }

  private static void checkSealName(String sealName) throws CodeException
  {
    if (StringUtil.isEmpty(sealName)) {
      throw new CodeException("C3010", "印章名不能为空");
    }
    if (StringUtil.checkValueInValid(sealName)) {
      throw new CodeException("C1012", "印章名称不能包含特殊字符");
    }
    if (sealName.length() > 32)
      throw new CodeException("C3048", "印章名称最大输入长度不能超过32位");
  }

  private static void checkSealPassword(String sealPassword) throws CodeException
  {
    if (StringUtil.isEmpty(sealPassword))
      throw new CodeException("C3011", "印章密码不能为空");
  }

  private static void checkSealPasswordValid(String sealPassword) throws CodeException
  {
    if (!StringUtil.checkPwdInValid(sealPassword))
      throw new CodeException("C3076", "密码长度在6-12之间，只能包含字符、数字和下划线");
  }

  private static void checkImageShape(String imageShape) throws CodeException {
    if (StringUtil.isEmpty(imageShape)) {
      throw new CodeException("C3042", "图片类型没有输入");
    }
    if (StringUtil.checkValueInValid(imageShape)) {
      throw new CodeException("C1015", "制作图片类型不正确");
    }

    if ((!"1".equals(imageShape)) && (!"3".equals(imageShape)))
      throw new CodeException("C2008", "不支持1-方章/3-长方形章以外的印章类型");
  }

  private static void checkImageShapeForAutoGenerateImage(String imageShape) throws CodeException
  {
    if (StringUtil.isEmpty(imageShape)) {
      throw new CodeException("C3042", "图片类型没有输入");
    }
    if (StringUtil.checkValueInValid(imageShape)) {
      throw new CodeException("C1015", "制作图片类型不正确");
    }

    if ((!"1".equals(imageShape)) && (!"2".equals(imageShape)) && (!"3".equals(imageShape)))
    {
      throw new CodeException("C1015", "制作图片类型不正确");
    }
  }

  private static void checkSealType(String sealType) throws CodeException {
    if (StringUtil.isEmpty(sealType))
      throw new CodeException("C3003", "签章类型不能为空");
    try
    {
      int type = Integer.parseInt(sealType);
      if ((type != 0) && (type != 1))
        throw new CodeException("C3035", "不支持0(企业)和1(个人)以外的印章类型");
    }
    catch (Exception e) {
      throw new CodeException("C3035", "不支持0(企业)和1(个人)以外的印章类型");
    }
  }

  public static void checkImageCode(String imageCode) throws CodeException {
    if (StringUtil.isEmpty(imageCode)) {
      throw new CodeException("C3042", "印模编码没有输入");
    }
    if (StringUtil.checkValueInValid(imageCode))
      throw new CodeException("C1012", "印模编码不能包含特殊字符");
  }

  public static void checkOrgCode(String orgCode) throws CodeException
  {
    if (StringUtil.isEmpty(orgCode)) {
      throw new CodeException("C8003", "机构编码不能为空");
    }
    if (StringUtil.checkValueInValid(orgCode)) {
      throw new CodeException("C1012", "机构编码不能包含特殊字符");
    }
    if (orgCode.length() > 32)
      throw new CodeException("C3050", "机构编码最大输入长度不能超过32位");
  }

  private static void checkSealOrg(String sealOrg) throws CodeException
  {
    if (StringUtil.isEmpty(sealOrg)) {
      throw new CodeException("C3042", "制章单位没有输入");
    }
    if (StringUtil.checkValueInValid(sealOrg)) {
      throw new CodeException("C1012", "制章单位不能包含特殊字符");
    }
    if (sealOrg.length() > 32)
      throw new CodeException("C3047", "制章单位最大输入长度不能超过32位");
  }

  public static void checkPageXY(String sealPage, String sealPageLx, String sealPageLy, int pdfMaxPage) throws CodeException
  {
    if ((StringUtil.isEmpty(sealPage)) || (StringUtil.isEmpty(sealPageLx)) || (StringUtil.isEmpty(sealPageLy))) {
      throw new CodeException("C3005", "按坐标签章时，页码和坐标不能为空");
    }

    if ((!StringUtil.checkNumber(sealPage, "+")) || (!StringUtil.checkNumber(sealPageLx, "+")) || (!StringUtil.checkNumber(sealPageLy, "+")))
    {
      throw new CodeException("C3043", "坐标签章的页数和坐标必须为正整数");
    }

    if (Integer.parseInt(sealPage) < 1) {
      throw new CodeException("C3036", "请输入正确的PDF页数");
    }

    if (Integer.parseInt(sealPage) > pdfMaxPage)
      throw new CodeException("C1013", "输入的页数不能超过文档总页数");
  }

  public static void checkCertAndPassword(byte[] certFile, String password)
    throws CodeException
  {
    if ((null == certFile) || (certFile.length == 0)) {
      throw new CodeException("C3009", "证书不能为空");
    }

    if (StringUtil.isEmpty(password)) {
      throw new CodeException("C8006", "证书密码不能为空");
    }
    try
    {
      if (SealCertUtil.isSM2(certFile))
        KeyUtil.getPrivateKeyFromSM2(certFile, password);
      else
        KeyUtil.getPrivateKeyFromPFX(certFile, password);
    }
    catch (PKIException e) {
      if (e.getMessage().contains("verifyMac faulture")) {
        throw new CodeException("C8005", "证书密码不正确");
      }
      if ("850605".equals(e.getErrCode())) {
        throw new CodeException("C8004", "证书内容不正确");
      }
      throw new CodeException(e.getErrCode(), e.getErrDesc());
    }
  }

  public static void checkLocationStyle(String type, String locationStyle) throws CodeException {
    if (("3".equals(type)) && (!"C".equals(locationStyle)) && (!"U".equals(locationStyle)) && (!"D".equals(locationStyle)) && (!"L".equals(locationStyle)) && (!"R".equals(locationStyle)))
    {
      throw new CodeException("C3060", "位置风格输入不正确");
    }
  }

  private static void checkAbsoluteAndFit(String waterMarkImage, String waterMarkText, String absoluteX, String absoluteY, String fitWidth, String fitHeight, String textFontSize) throws CodeException
  {
    if ((StringUtil.isEmpty(absoluteX)) || (StringUtil.isEmpty(absoluteY))) {
      throw new CodeException("C3061", "水印X坐标/Y坐标不能为空");
    }
    if (!StringUtil.isEmpty(waterMarkImage)) {
      if ((StringUtil.isEmpty(fitWidth)) || (StringUtil.isEmpty(fitHeight)))
        throw new CodeException("C3062", "水印图片非空时，水印宽度/高度不能为空");
    }
    else if ((!StringUtil.isEmpty(waterMarkText)) && 
      (!StringUtil.isNumeric(textFontSize)))
      throw new CodeException("C3063", "水印文本非空时，水印文本字体大小只能输入正整数");
  }

  public static void checkParamForBindClientSeal(MakeAndBindClientSealBean makeAndBindClientSealBean)
    throws CodeException
  {
    checkCertDnAndSn(makeAndBindClientSealBean.getCertDn(), makeAndBindClientSealBean.getCertSn());
    checkUserName(makeAndBindClientSealBean.getName(), makeAndBindClientSealBean.getRealName());
  }

  public static void checkParamForQRCode(String qrCodeContent, String width, String height, String pageNo, String lx, String ly, int pageNoMax) throws CodeException
  {
    checkQrCodeContent(qrCodeContent);
    checkImageWidth(width);
    checkImageHeight(height);
    checkPageXY(pageNo, lx, ly, pageNoMax);
  }

  private static void checkQrCodeContent(String qrCodeContent) throws CodeException {
    if (StringUtil.isEmpty(qrCodeContent))
      throw new CodeException("C3042", "二维码内容没有输入");
  }

  public static void checkFields(String fieldType, String filedId, String fieldValue) throws CodeException
  {
    if (StringUtil.isEmpty(fieldType)) {
      throw new CodeException("C3042", "合成类型没有输入");
    }
    if ((!"text".equals(fieldType)) && (!"checkbox".equals(fieldType))) {
      throw new CodeException("C3075", "合成类型输入不正确(只支持text/checkbox)");
    }

    if (StringUtil.isEmpty(filedId)) {
      throw new CodeException("C3042", "合成字段ID没有输入");
    }
    if (StringUtil.isEmpty(fieldValue))
      throw new CodeException("C3042", "合成字段值没有输入");
  }

  public static void checkParamForImage(String imageValue, String imageWidth, String imageHeight, String pageNo, String lx, String ly, int pageNoMax)
    throws CodeException
  {
    checkImageValue(imageValue);
    checkImageWidth(imageWidth);
    checkImageHeight(imageHeight);
    checkPageXY(pageNo, lx, ly, pageNoMax);
  }

  private static void checkImageValue(String imageValue) throws CodeException {
    if (StringUtil.isEmpty(imageValue))
      throw new CodeException("C3042", "图片base64编码没有输入");
  }

  public static void checkParamForImage2(String imageValue, String imageWidth, String imageHeight, String type, String keyword, String locationStyle)
    throws CodeException
  {
    checkImageValue(imageValue);
    checkImageWidth(imageWidth);
    checkImageHeight(imageHeight);
    checkKeyword(type, keyword);
    checkLocationStyle(type, locationStyle);
  }

  public static void checkTypeForImage(String type) throws CodeException {
    if (StringUtil.isEmpty(type)) {
      throw new CodeException("C3042", "类型没有输入");
    }
    if ((!"2".equals(type)) && (!"3".equals(type)))
      throw new CodeException("C3074", "合成字段类型输入不正确(只支持2：坐标/3：关键字)");
  }
}